﻿/*******************************************************
 * 
 * 作者：胡庆访
 * 创建时间：201001
 * 说明：物料入库服务
 * 运行环境：.NET 4.0
 * 版本号：1.0.0
 * 
 * 历史记录：
 * 创建文件 胡庆访 201001
 * 
*******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Entity;
using BPM.Common;
using System.Transactions;

namespace BPM.Service
{
	/// <summary>
	/// 物料入库服务
	/// </summary>
	public class MaterialInDeliveryService : ServiceBase<MaterialInDelivery>
	{
		/// <summary>
		/// 把指定的物料进行入库操作。
		/// </summary>
		/// <param name="materialsAmount">
		/// 物料集合
		/// Key : MaterialId
		/// Value : Amount
		/// </param>
		/// <param name="purchaseOrderId">
		/// 本次物料入库所对应的进货单
		/// </param>
		/// <param name="currentUser">
		/// 当前操作者
		/// </param>
		/// <returns></returns>
		public Result MaterialIn(Dictionary<int, int> materialsAmount, int purchaseOrderId, User currentUser)
		{
			var oderSvc = new PurchaseOrderService();
			var order = oderSvc.GetById(purchaseOrderId);
			if (order == null) return "没有找到相应的订货单。";
			if (!order.IsRequiredMaterials(materialsAmount)) return "此订货单并不需要购买这些物料，操作失败。";

			using (var tran = new TransactionScope())
			{
				#region 修改库存

				var mdSvc = new MaterialDefinitionService();
				foreach (var kv in materialsAmount)
				{
					var material = mdSvc.GetById(kv.Key);
					if (material == null) return "没有找到相应的物料。";
					material.CurrentAmount += kv.Value;
					var result = mdSvc.Save(material);
					if (!result) return result;
				}

				#endregion

				#region 添加进货记录

				var now = DateTime.Now;
				var inDelivery = new MaterialInDelivery()
				{
					InDeliveryTime = now,
					ItemList = new List<MaterialInDeliveryItem>(),
					PurchaseOrderId = order.Id,
					UserId = currentUser.Id
				};

				foreach (var kv in materialsAmount)
				{
					inDelivery.ItemList.Add(new MaterialInDeliveryItem()
					{
						MaterialDefinitionId = kv.Key,
						Amount = kv.Value
					});
				}

				var svc = new MaterialInDeliveryService();
				var res = svc.Save(inDelivery);
				if (!res) return res;

				#endregion

				#region 修改订货单对象

				var leftAmount = order.GetLeftAmout();
				var inputAmount = materialsAmount.Sum(kv => kv.Value);
				if (leftAmount == inputAmount)
				{
					var context = this.NewContext();
					order = context.Set<PurchaseOrder>().First(o => o.Id == purchaseOrderId);
					order.IsFinished = true;
					context.Save();
				}

				#endregion

				tran.Complete();
				return true;
			}
		}

		/// <summary>
		/// 把指定的物料进行出库操作。
		/// </summary>
		/// <param name="materialsAmount">
		/// 物料集合
		/// Key : MaterialId
		/// Value : Amount
		/// </param>
		/// <param name="currentUser">
		/// 当前操作者
		/// </param>
		/// <returns></returns>
		public Result MaterialOut(Dictionary<int, int> materialsAmount, User currentUser)
		{
			using (var tran = new TransactionScope())
			{
				#region 修改库存

				var mdSvc = new MaterialDefinitionService();
				foreach (var kv in materialsAmount)
				{
					var material = mdSvc.GetById(kv.Key);
					if (material == null) return "没有找到相应的物料。";
					material.CurrentAmount -= kv.Value;
					if (material.CurrentAmount < 0) return material.Name + "的现有存量不足，出货失败。";
					var result = mdSvc.Save(material);
					if (!result) return result;
				}

				#endregion

				#region 添加出货记录

				var mod = new MaterialOutDelivery()
				{
					ItemList = new List<MaterialOutDeliveryItem>(),
					OperatorId = currentUser.Id,
					OutDeliveryTime = DateTime.Now
				};

				foreach (var kv in materialsAmount)
				{
					mod.ItemList.Add(new MaterialOutDeliveryItem()
					{
						MaterialDefinitionId = kv.Key,
						Amount = kv.Value,
						MaterialOutDelivery = mod
					});
				}

				var res = new MaterialOutDeliveryService().Save(mod);
				if (!res) return res;

				#endregion

				tran.Complete();
				return true;
			}
		}
	}
}
